package com.millstein.tsinglog.result;

import com.millstein.tsinglog.commons.constant.SystemConstant;
import com.millstein.tsinglog.model.pojo.Permission;
import com.millstein.tsinglog.model.pojo.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.crazycake.shiro.AuthCachePrincipal;

import java.io.Serializable;
import java.util.*;

/**
 * 做权限验证的用户信息
 */
@Data
@NoArgsConstructor
public class ProfileResult implements Serializable, AuthCachePrincipal {
    private Long id;
    private String username;
    /**
     * 由于管理员的回复不能修改，所以这个属性的作用是用来判断这条评论是否是管理员回复
     * 如果是，那么此条评论不能被修改；如果不是，才能被修改
     */
    private List<String> allowCheckCommentUser;
    private Map<String, Object> permissionMap = new HashMap<>();

    public ProfileResult(User user, List<Permission> permissions, List<String> allowCheckCommentUser) {
        this.id = user.getId();
        this.username = user.getUsername();
        this.allowCheckCommentUser = allowCheckCommentUser;

        /**
         * 存放menus是为了动态加载菜单，需要封装成专门的格式
         */
        Set<Map<String, Object>> menus = new LinkedHashSet<>();
        /**
         * 存放buttons是为了在页面上显示按钮，只需要对应的permissionCode即可
         */
        Set<String> buttons = new LinkedHashSet<>();

        for (Permission permission : permissions) {
            if (permission.getPermissionType() == SystemConstant.PERMISSION_TYPE_MENU) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", permission.getId());
                map.put("name", permission.getPermissionName());
                map.put("icon", permission.getIcon());
                map.put("to", permission.getUrl());
                map.put("parentId", permission.getParentId());

                if (permission.getParentId() == 0) {
                    map.put("isSubShow", false);
                }
                menus.add(map);
            }

            if (permission.getPermissionType() == SystemConstant.PERMISSION_TYPE_BUTTON) {
                buttons.add(permission.getPermissionCode());
            }
        }

        permissionMap.put("menus", menus);
        permissionMap.put("buttons", buttons);
    }

    @Override
    public String getAuthCacheKey() {
        return null;
    }
}
